//------------------------------------------------------------------------------ 
// <copyright file="JAccordion.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 

namespace FmjQuery
{
    using System.Text;
    using System.Web.UI;

    /// <summary>
    /// Accordion Control
    /// </summary>
    [ToolboxData("<{0}:JAccordion runat=server></{0}:JAccordion")]
    [NonVisualControl]
    public class JAccordion : JQueryBaseControl
    {
        #region Fields
        /// <summary>
        /// Collection of jTab
        /// </summary>
        private AccordionPanels tlistaccordion;
        #endregion

        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JAccordion class.
        /// </summary>
        public JAccordion()
        {
            this.IsRenderControl = true;
        }
        #endregion

        #region Property

        /// <summary>
        /// Gets list of accordion panel
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public AccordionPanels Accordions 
        { 
            get
            {
                if (this.tlistaccordion == null)
                {
                    this.tlistaccordion = new AccordionPanels();
                }

                return this.tlistaccordion;
            }
        }

        /// <summary>
        /// Gets or sets id of selected tab
        /// </summary>
        public string SelectedID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the highest content part is used as height reference for all other parts
        /// </summary>
        public bool AutoHeight
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether clears height and overflow styles after finishing animations
        /// </summary>
        public bool ClearStyle
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether collapsible
        /// </summary>
        public bool Collapsible
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of event that active panel
        /// </summary>
        public string ClientActiveEvent
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the accordion completely fills the height of the parent element
        /// </summary>
        public bool FillSpace
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether looks for the anchor that matches location.href and activates it
        /// </summary>
        public bool Navigation
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when change selected panel
        /// </summary>
        /// <example>
        ///     function(event, ui)
        ///     ui.options  options used to intialize this widget
        ///     ui.newHeader  jQuery, activated header
        ///     ui.oldHeader  jQuery, previous header
        ///     ui.newContent  jQuery, activated content
        ///     ui.oldContent  jQuery, previous content
        /// </example>
        public string ClientChangePanel
        {
            get; 
            set;
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Return list of webResource
        /// </summary>
        /// <returns>web resources</returns>
        public override JWebSources GetResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            if (this.Enabled)
            {
                this.Manager.Accordion = true;

                // add child control
                foreach (JAccordionPanel pnl in this.tlistaccordion)
                {
                    this.Controls.Add(pnl);
                }

                StringBuilder builder = new StringBuilder("$(document).ready(function(){$('#");

                builder.Append(this.ClientID);
                builder.Append("').accordion({header:'h6'");

                if (!string.IsNullOrEmpty(this.SelectedID))
                {
                    builder.AppendFormat(",active: {0}", this.tlistaccordion.FindIndex(a => a.ID.Equals(this.SelectedID)));
                }

                if (this.AutoHeight)
                {
                    builder.Append(",autoHeight: true");
                }

                if (this.ClearStyle)
                {
                    builder.Append(",clearStyle: true");
                }

                if (this.Collapsible)
                {
                    builder.Append(",collapsible: true");
                }

                if (this.FillSpace)
                {
                    builder.Append(",fillSpace: true");
                }

                if (this.Navigation)
                {
                    builder.Append(",navigation: true");
                }

                if (!string.IsNullOrEmpty(this.ClientActiveEvent))
                {
                    builder.AppendFormat(",event: '{0}'", this.ClientActiveEvent);
                }

                builder.Append("})");

                // event
                if (!string.IsNullOrEmpty(this.ClientChangePanel))
                {
                    builder.AppendFormat(".bind('accordionchange', {0}", this.ClientChangePanel);
                }

                builder.Append(";});");

                // create script
                Utility.RegisterStartUpScript(this.Page, string.Concat("accordion_", this.ClientID), builder.ToString());
            }
        }

        /// <summary>
        /// Renders the control to the specified HTML writer.
        /// </summary>
        /// <param name="writer">The System.Web.UI.HtmlTextWriter object that receives the control content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            string dimension = string.Empty;

            if (this.Height.Value > 0)
            {
                dimension = string.Concat("height:", this.Height.Value, "px");
            }

            if (this.Width.Value > 0)
            {
                if (!string.IsNullOrEmpty(dimension))
                {
                    dimension += ";";
                }

                dimension += string.Concat("width:", this.Width.Value, "px");
            }

            if (!string.IsNullOrEmpty(dimension))
            {
                dimension = string.Concat("style='", dimension, "'");
            }

            // render header
            new LiteralControl(string.Format("<div id='{0}' {1}>", this.ClientID, dimension)).RenderControl(writer);

            foreach (JAccordionPanel pnl in this.tlistaccordion)
            {
                pnl.HeaderContainer.RenderControl(writer);

                new LiteralControl("<div>").RenderControl(writer);
                pnl.ContentContainer.RenderControl(writer);
                new LiteralControl("</div>").RenderControl(writer);
            }

            new LiteralControl("</div>").RenderControl(writer);
        }
        #endregion
    }
}